﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using rkShared.Serialization;
using rkShared.BEncode;
using System.Globalization;
using rkShared.Net;


namespace rkShared
{
	//Oyuncunun tankı ile ilgili class
    [Serializable]
    public class Tank : IBinarySerializable
	{
		private int hitpoints = 20;
        private float canonRotation = 0.0f;
		private Vector3 position = new Vector3();
		private float[] color = new float[3];
		//patlama animasyonlar
		private ArrayList explosions = new ArrayList();
		
		//tankın mermileri
		public ArrayList bullets = new ArrayList();
		
		//oyuncu sol mouse tuşuna basınca bu metod yeni bir 
		//mermi yaratıp listeye ekliyor.
		public void fireBullet(Vector3 destination,System.Media.SoundPlayer sp)
		{
			Bullet newBullet = new Bullet();
			//play sound
			sp.Play();
		
						
			//the bullets initial position is the same as the tank
			newBullet.Position.Copy(position);
			newBullet.Position.y+= 5.0f;
			newBullet.Destination.Copy(destination);
			newBullet.calculateVelocity();

			bullets.Add(newBullet);
		}

		public void update(float elapsedTime)
		{
			Bullet bullet = null;
            
            
            //check if a bullet reached its destination
            for(int i=0; i<bullets.Count; ++i){
				bullet = (Bullet)bullets[i];
				bullet.Position += (bullet.Velocity*elapsedTime*300.0f);
				//if the bullet reaches the destination remove it from the list
				if(UMath.Distance(bullet.Position,bullet.Destination) < 10.0f){
					explosions.Add(new Billboard(bullet.Position,20,0,10,false));
					bullets.RemoveAt(i);
				}
			}
            //update explosions or remove them if they are done
			Billboard billboard = null;
			for(int i=0;i<explosions.Count;++i){
				billboard = (Billboard)explosions[i];
				billboard.fTile += elapsedTime*15.0f;
				if(billboard.fTile > billboard.fTotal)
					explosions.RemoveAt(i);

			}
		}




		public int Hitpoints
        {
            get { return hitpoints; }
            set { hitpoints = value; }
        }

		public Vector3 Position
		{
			get{return position;}
			set{position = value;}
		}
		public float CanonRotation
		{
			get{return canonRotation;}
			set{canonRotation = value;}
		}
		public ArrayList Explosions
		{
			get{return explosions;}
		}
		public void setColor(float r,float g,float b)
		{
			color[0] = r;
			color[1] = g;
			color[2] = b;
		}
		public float[] getColor()
		{
			return color;
		}

        #region IBinarySerializable Members

        public void SerializeToDictionary(rkShared.BEncode.BEncodedDictionary dictionary)
        {
            dictionary.Add("p", this.position);
            dictionary.Add("cr", this.canonRotation.ToString(CultureInfo.InvariantCulture));
            dictionary.Add("c", new Vector3(color[0], color[1], color[2]));
            dictionary.Add("h", this.hitpoints);

            BEncodedList _list = new BEncodedList();
            foreach (Billboard b in Explosions)
            {
                _list.Add(b);
            }
            dictionary.Add("e", _list);
        }
        public void DeserializeFromDictionary(rkShared.BEncode.BEncodedDictionary dict)
        {
            this.position = dict.GetIBinarySerializable<Vector3>("p");
            string _canonRotation = dict.GetString("cr");
            this.hitpoints = dict.GetInt("h");

            this.CanonRotation = float.Parse(_canonRotation, CultureInfo.InvariantCulture);
            Vector3 _color = (dict.GetIBinarySerializable<Vector3>("c"));
            this.color[0] = _color.x;
            this.color[1] = _color.y;
            this.color[2] = _color.z;

            this.explosions = new ArrayList();

            BEncodedList _explosions = dict["e"] as BEncodedList;
            foreach (BEncodedDictionary d in _explosions)
            {
                Billboard b = new Billboard();
                b.DeserializeFromDictionary(d);
                this.explosions.Add(b);
            }
        }
        #endregion

        public void setColor(Vector3 vector3)
        {
            this.color[0] = vector3.x;
            this.color[1] = vector3.y;
            this.color[2] = vector3.z;
        }
    }
}
